package com.gm.baseplatform.core.utils;


public class PageUtil {

	private int pageNo;
	private int pageRecordNo;
	private int totalPageNo;
	private int totalRecordNo;
	
	private int maxIndexRange = 10;
	private int pageIndexBeginRange;
	private int pageIndexEndRange;
	
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public int getPageRecordNo() {
		return pageRecordNo;
	}
	public void setPageRecordNo(int pageRecordNo) {
		this.pageRecordNo = pageRecordNo;
	}
	public int getTotalPageNo() {
		return totalPageNo;
	}
	public void setTotalPageNo(int totalPageNo) {
		this.totalPageNo = totalPageNo;
	}
	public int getTotalRecordNo() {
		return totalRecordNo;
	}
	public void setTotalRecordNo(int totalRecordNo) {
		this.totalRecordNo = totalRecordNo;
	}
	public int getPageIndexBeginRange() {
		return pageIndexBeginRange;
	}
	public void setPageIndexBeginRange(int pageIndexBeginRange) {
		this.pageIndexBeginRange = pageIndexBeginRange;
	}
	public int getPageIndexEndRange() {
		return pageIndexEndRange;
	}
	public void setPageIndexEndRange(int pageIndexEndRange) {
		this.pageIndexEndRange = pageIndexEndRange;
	}
	
	public Integer getTotalPages() {
		return (this.totalRecordNo + this.pageRecordNo - 1) / this.pageRecordNo;
	}
	
	public boolean isNextPage() {
		return getTotalPages() != pageNo ? true : false;
	}
	
	public boolean isPreviousPage() {
		return pageNo > 1 ? true : false;
	}
	
	public void generateIndexPage(){
		generateIndexPageBegin();
		generateIndexPageEnd();
		this.totalPageNo = this.getTotalPages().intValue();
	}

	public int getFirstResult(){
		return (this.getPageNo() - 1) * this.getPageRecordNo();
	}
	
    public int getMaxResults(){
    	return this.getPageRecordNo();
	}
    
    public int getBeginRowNo(){
    	if(this.getPageNo() == 1){
    		return (this.getPageNo() - 1) * this.getPageRecordNo();
    	}else{
    		return (this.getPageNo() - 1) * this.getPageRecordNo() + 1;
    	}
	}
    
    public int getEndRowNo(){
    	if(this.getPageNo() == 1){
    	  return this.getBeginRowNo() + this.getPageRecordNo();
    	}else{
    		return this.getBeginRowNo() + this.getPageRecordNo() - 1;
    	}
	}
    
	private void generateIndexPageBegin() {
		int indexPageBegin = 1;
		
		int n = maxIndexRange / 2;
		
		if (maxIndexRange > getTotalPages()) {
			indexPageBegin = 1;
		} else if (!isNextPage() || pageNo + n > getTotalPages()) {
			indexPageBegin = getTotalPages() - (maxIndexRange - 1);
		} else if (isNextPage()) {
			if (((pageNo + maxIndexRange) / 2) < (maxIndexRange - 1)) {
				indexPageBegin = 1;
			} else {
				indexPageBegin = pageNo - n <= 0 ? 1 : pageNo - n;
			}
		} 
		this.pageIndexBeginRange = indexPageBegin;
	}

	private void generateIndexPageEnd() {
		int indexPageEnd = 1;
		
		int n = maxIndexRange / 2;
		
		if (maxIndexRange > getTotalPages() || !isNextPage()) {
			indexPageEnd = getTotalPages();
		} else if (!isPreviousPage()) {
			indexPageEnd = maxIndexRange;
		} else if (pageNo + n <= getTotalPages()) {
			if (((pageNo + maxIndexRange) / 2) < (maxIndexRange - 1)) {
				indexPageEnd = maxIndexRange;
			} else {
				indexPageEnd = pageNo + n;
			}
		} else {
			indexPageEnd = getTotalPages();
		}
		this.pageIndexEndRange = indexPageEnd;
	}
	
    public static void main(String[] args){
    	
    	PageUtil page = new PageUtil();
    	page.setPageNo(49);
    	page.setPageRecordNo(10);
    	page.setTotalRecordNo(500);
    	
    	page.generateIndexPage();
    	
    	System.out.println(page.getTotalPages());
    	System.out.println(page.getPageIndexBeginRange());
    	System.out.println(page.getPageIndexEndRange());
    }

}
